﻿using Exam.BLL;
using Exam.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exam.Controllers
{
    public class HomeController : Controller
    {
        ShoppingBLL bll;
        public HomeController()
        {
            bll = new ShoppingBLL();
        }
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string username,string pwd)
        {
            if (bll.Login(username,pwd))
            {
                return RedirectToAction("Manager");
            }
            return View();
        }

        public ActionResult Manager()
        {
            return View(bll.GetProducts());
        }

        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(Product product)
        {
            product.PostDate = Convert.ToDateTime("2010-02-02");
            bll.AddProduct(product);
            return RedirectToAction("Manager");
        }

        [ActionName("Remove")]
        public ActionResult Remove(int id)
        {
            bll.RemoveProduct(id);
            return RedirectToAction("Manager");
        }

        public ActionResult Modify(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Manager");
            }
            var product = bll.GetProducts(id);

            return View(product);
        }

        [HttpPost]
        public ActionResult Modify(Product product)
        {
            bll.ModifyProduct(product);
            return RedirectToAction("Manager");
        }
    }
}